package org.grapes.wine.api.server.method.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.grapes.wine.api.bean.GetAdjoinedLandscapeLocationBean;
import org.grapes.wine.api.bean.MethodBean;
import org.grapes.wine.api.bean.ObjectFactory;
import org.grapes.wine.api.method.EWineMethod;
import org.grapes.wine.api.server.method.handler.RenderDocument;
import org.grapes.wine.api.server.model.DAOName;
import org.grapes.wine.api.server.model.dao.AdjoinedLandscapeDAO;
import org.grapes.wine.api.server.persistence.DAOOperate;
import org.w3c.dom.Document;

public class GetAdjoinedLandscapeLocation extends RenderDocument {

	static Logger logger = Logger.getLogger(GetAdjoinedLandscapeLocation.class);
	private static List param = new ArrayList();
	static {
		param.add("adjoinedLandscapeName");
	}

	public Document execute(HttpServletRequest request) throws Exception {
		boolean paramMatch = false;

		String adjoinedLandscapeName = request
				.getParameter("adjoinedLandscapeName");
		AdjoinedLandscapeDAO adjoinedLandscapeDAO = null;

		List list = DAOOperate.queryList(DAOName.AdjoinedLandscape);

		for (Iterator it = list.iterator(); it.hasNext();) {
			adjoinedLandscapeDAO = (AdjoinedLandscapeDAO) it.next();
			if (adjoinedLandscapeDAO.getAdjoinedLandscapeName().equals(
					adjoinedLandscapeName)) {
				logger.info("adjoinedLandscapeName: "
						+ adjoinedLandscapeDAO.getAdjoinedLandscapeName());
				paramMatch = true;
				break;
			}
		}

		if (paramMatch) {
			// put data input bean
			ObjectFactory factory = new ObjectFactory();
			GetAdjoinedLandscapeLocationBean bean = factory
					.createGetAdjoinedLandscapeLocationBean();
			bean.setAdjoinedLandscapeLocation(adjoinedLandscapeDAO
					.getLocation());
			MethodBean methodBean = new MethodBean();
			methodBean.setBean(bean);
			methodBean.setClazz(GetAdjoinedLandscapeLocationBean.class);
			return initDocumentByJAXB(methodBean);
		}
		return null;
	}

	public EWineMethod getMethodName() {
		return EWineMethod.GET_ADJOINED_LANDSCAPE_LOCATION;
	}

	public List getParam() {
		return param;
	}
}
